package com.zenithmind.gateway.config;

import org.springframework.stereotype.Component;
import lombok.extern.slf4j.Slf4j;

import java.util.Map;
import java.util.List;
import java.util.HashMap;

/**
 * 默认路由配置提供者
 * 专门负责提供默认的服务路由映射配置
 * 遵循单一职责原则：只负责默认配置的提供
 */
@Slf4j
@Component
public class DefaultRouteConfigProvider {

    /**
     * 获取默认路由配置
     * 当配置文件中没有定义时使用这些默认值
     */
    public Map<String, List<String>> getDefaultServiceRoutes() {
        log.debug("提供默认服务路由配置");
        
        Map<String, List<String>> defaults = new HashMap<>();
        
        // 1. 用户相关服务默认路由
        defaults.put("user-service", 
            List.of("user", "auth", "verification", "profile", "role", "permission", "dashboard", "stats"));
            
        // 2. 内容管理服务默认路由
        defaults.put("article-service", 
            List.of("article", "article-category", "category", "tag"));
            
        defaults.put("note-service", 
            List.of("note", "notebook", "note-tag"));
            
        defaults.put("news-service", 
            List.of("news", "announcement"));
            
        defaults.put("document-service", 
            List.of("document", "template", "doc"));
            
        defaults.put("mall-service", 
            List.of("mall", "product", "order", "payment"));
            
        // 3. 教育学习服务默认路由
        defaults.put("classroom-service", 
            List.of("course", "courses", "chapter", "lesson"));
            
        defaults.put("exam-service", 
            List.of("exam", "question", "record"));
            
        defaults.put("coding-service", 
            List.of("problem", "submission", "code"));
            
        defaults.put("library-service", 
            List.of("library", "book", "borrow"));
            
        // 4. 存储服务默认路由
        defaults.put("disk-service", 
            List.of("disk", "file", "folder", "share"));
            
        defaults.put("version-service", 
            List.of("version", "repository", "commit", "branch"));
            
        // 5. 通信服务默认路由
        defaults.put("chat-service", 
            List.of("chat", "message", "room"));
            
        defaults.put("ai-chat-service", 
            List.of("aiChat", "modelConfig", "ai", "model"));
            
        // 6. 工具服务默认路由
        defaults.put("task-service", 
            List.of("task", "schedule", "reminder"));
            
        return defaults;
    }
    
    /**
     * 合并配置的路由和默认路由
     * @param configuredRoutes 配置文件中的路由
     * @return 合并后的路由配置
     */
    public Map<String, List<String>> mergeWithDefaults(Map<String, List<String>> configuredRoutes) {
        Map<String, List<String>> result = new HashMap<>(getDefaultServiceRoutes());
        
        if (configuredRoutes != null && !configuredRoutes.isEmpty()) {
            result.putAll(configuredRoutes);
            log.info("合并了 {} 个配置的路由和 {} 个默认路由", 
                configuredRoutes.size(), getDefaultServiceRoutes().size());
        } else {
            log.info("使用默认路由配置，共 {} 个服务", result.size());
        }
        
        return result;
    }
}
